<?php
/**
 * Created by PhpStorm.
 * User: xiaojun
 * Date: 15/12/22
 * Time: 下午3:08
 */

namespace Home\Controller;
use Common\Controller\BaseController;
use Think\Controller;
class DrugController extends BaseController {

    /**
     * 药品详情入口函数
     */
    public function index(){
        if(!IS_GET){
            $this->error('非法访问');
        }
        //获取药品的ID
        $id = I('get.id');
        if(empty($id)){
            $this->error('获取药品信息失败，请稍后重试');
        }
        if(strlen($id) != 24){
            $this->error('未找到该药品');
        }
        //开始获取药品的详情
        $drug = json_decode($this->http->curl_request(BASE_URL.'drug/'.$id),true);
        $drug_info = [];
        if($drug){
            if($drug['code'] != 200){
                $this->error($drug['message']);
            }else{
                $drug_info = $drug['results'];
            }
        }else{
            $this->error('获取药品详情失败');
        }
        //将药品信息给到前台
        $this->assign('drug',$drug_info);

        //获取点评数据
        $comments = json_decode($this->http->curl_request(BASE_URL.'comment/drug/'.$id.'?with_count=1&p=1&n=2'),true);
        $comment_info = [];
        if($comments){
            if($comments['code'] != 200){
                $this->error($comments['message']);
            }else{
                $comment_info = $comments['results'][0]['comment'];
            }
        }else {
            $this->error('获取药品详情失败');
        }
        $this->assign('comment',$comment_info);
        $this->assign('comment_count',$comments['results'][0]['comment_count']);
        $this->display('index');
    }

}